package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Dev;
import com.ruoyi.system.domain.Devparastruct;
import com.ruoyi.system.service.DevService;
import com.ruoyi.system.service.DevparastructService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 通用设备
 */
@RestController
@RequestMapping("/dev")
public class DevController extends BaseController {
    @Autowired
    private DevService devService;
    @Autowired
    private DevparastructService devparastructService;

    /**
     * 新增设备
     * @param dev
     */
    @PostMapping("/addDev")
    public void addDev(@RequestBody(required = false) Dev dev) {
        //查询的表刚增进去数据的id
        Dev devIds = devService.Listid();
        Integer devId=devIds.getDevID();
        String imei = dev.getIMEI();
        Integer flastId=dev.getFlastId();
        int AnGrade = 0;
        int SwGrade = 0;
        //新增数据到devparastruct
        for (Devparastruct devparastruct : dev.getDevparastructs()) {
            devparastruct.setDevId(devId);
            devparastruct.setIMEI(imei);
            devparastruct.setFlastId(flastId);
            // 合并paraNo1的值为二进制
            Integer paraNo1 = devparastruct.getParaNo1();
            int paraNo = paraNo1;

            if (devparastruct.getParaType() == 1) {
                SwGrade |= 1 << paraNo; // 使用按位左移运算符将1左移paraNo位
            } else if (devparastruct.getParaType() == 2) {
                AnGrade |= 1 << paraNo;
            }
            devparastructService.adddev(devparastruct);
        }
        // 将AnGrade和SwGrade存储到Dev对象中
        dev.setAnGrade(AnGrade);
        dev.setSwGrade(SwGrade);
        //新增设备到dev表
        devService.addDev(dev);
    }
    @DeleteMapping("/deleteDev")
    public AjaxResult deleteDev(Integer DevId){
        devparastructService.deleteDev(DevId);
        return toAjax(devService.deleteDev(DevId));
    }
    /**
     * 修改设备
     */
    @PutMapping("/updateDev")
    public AjaxResult updDev(@RequestBody(required = false) Dev dev) {
        // 更新设备到dev表
        if (dev.getDevId() != null) {
            // 根据devId查询devparastruct
            List<Devparastruct> selectDevidList = devparastructService.selectDevid(dev.getDevId());
            // 删除不需要的数据
            for (Devparastruct selectDevid : selectDevidList) {
                boolean needToDelete = true;
                for (Devparastruct devparastruct : dev.getDevparastructs()) {
                    if (selectDevid.getDevId().equals(dev.getDevId())
                            && selectDevid.getParaNo1().equals(devparastruct.getParaNo1())) {
                        needToDelete = false;
                        break;
                    }
                }

                if (needToDelete) {
                    devparastructService.deleteParaNo1(dev.getDevId(), selectDevid.getParaNo1());
                }
            }
            // 新增数据
            for (Devparastruct devparastruct : dev.getDevparastructs()) {
                boolean needToAdd = true;
                for (Devparastruct selectDevid : selectDevidList) {
                    if (dev.getDevId().equals(selectDevid.getDevId())
                            && devparastruct.getParaType().equals(selectDevid.getParaType())
                            && devparastruct.getParaNo1().equals(selectDevid.getParaNo1())) {
                        needToAdd = false;
                        break;
                    }
                }

                if (needToAdd) {
                    devparastruct.setDevId(dev.getDevId());
                    devparastruct.setIMEI(selectDevidList.get(0).getIMEI()); // 设置IMEI值
                    devparastructService.adddev(devparastruct);
                }
            }
        }

        return null;
    }
}
